search.js ➔ searchPage   D
last analyzed

Complexity

Conditions 13

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
dl 0
loc 14
rs 4.2
c 0
b 0
f 0
eloc 8

How to fix   Complexity   

Complexity

Complex classes like search.js ➔ searchPage often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
// BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
2
//
3
// Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below).
4
//
5
// This program is free software; you can redistribute it and/or modify it under the
6
// terms of the GNU Lesser General Public License as published by the Free Software
7
// Foundation; either version 3.0 of the License, or (at your option) any later
8
// version.
9
//
10
// BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
11
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
12
// PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
13
//
14
// You should have received a copy of the GNU Lesser General Public License along
15
// with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
16
17
$(document).on('turbolinks:load', function(){
18
  var controller = $("body").data('controller');
19
  var action = $("body").data('action');
20
21
  if ((controller == "admins" && action == "index") || 
22
      (controller == "rooms" && action == "show") || 
23
      (controller == "rooms" && action == "update") ||
24
      (controller == "rooms" && action == "join") || 
25
      (controller == "users" && action == "recordings") ||
26
      (controller == "admins" && action == "server_recordings")) {
27
    // Submit search if the user hits enter
28
    $("#search-input").keypress(function(key) {
29
      if (key.which == 13) {
30
        searchPage()
31
      }
32
    })
33
34
    // Add listeners for sort
35
    $("th[data-order]").click(function(data){
36
      var header_elem = $(data.target)
37
38
      if(header_elem.data('order') === 'asc'){ // asc
39
        header_elem.data('order', 'desc');
40
      }
41
      else if(header_elem.data('order') === 'desc'){ // desc
42
        header_elem.data('order', 'none');
43
      }
44
      else{ // none
45
        header_elem.data('order', 'asc');
46
      }
47
48
      var search = $("#search-input").val();
49
50
      var url = window.location.pathname + "?page=1&search=" + search + "&column=" + header_elem.data("header") +
51
       "&direction=" + header_elem.data('order')
52
53
      window.location.replace(addRecordingTable(url))
54
    })
55
56
    if(controller === "rooms" && action === "show"){
57
      $(".page-item > a").each(function(){
58
        if(!$(this).attr('href').endsWith("#")){
59
          $(this).attr('href', $(this).attr('href') + "#recordings-table")
60
        }
61
      })
62
    }
63
  }
64
})
65
66
// Searches the user table for the given string
67
function searchPage() {
68
  var search = $("#search-input").val();
69
70
  // Check if the user filtered by role
71
  var role = new URL(location.href).searchParams.get('role')
72
  var tab = new URL(location.href).searchParams.get('tab')
73
74
  var url = window.location.pathname + "?page=1&search=" + search
75
76
  if (role) { url += "&role=" + role } 
77
  if (tab) { url += "&tab=" + tab } 
78
79
  window.location.replace(addRecordingTable(url));
80
}
81
82
// Clears the search bar
83
function clearSearch() {
84
  var role = new URL(location.href).searchParams.get('role')
85
  var tab = new URL(location.href).searchParams.get('tab')
86
87
  var url = window.location.pathname + "?page=1"
88
89
  if (role) { url += "&role=" + role } 
90
  if (tab) { url += "&tab=" + tab } 
91
  
92
  window.location.replace(addRecordingTable(url));
93
94
  var search_params = new URLSearchParams(window.location.search)
0 ignored issues
show
Bug introduced by
The variable URLSearchParams seems to be never declared. If this is a global, consider adding a /** global: URLSearchParams */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Unused Code introduced by
The variable search_params seems to be never used. Consider removing it.
Loading history...
95
}
96
97
function addRecordingTable(url) {
98
  if($("body").data('controller') === "rooms" && $("body").data('action') === "show") { 
99
    url += "#recordings-table"
100
  }
101
  return url
102
}
103